<template>
  <div class="bid-info">
      <h3>
        <div v-if="status===1">
          <el-button size="mini" type="primary" @click="shortListShow"  v-if="showCheck">中标审核</el-button>
        </div>
        <!-- <el-button size="mini" type="primary" @click="shortListShow" v-if="showCheck" :disabled="vendorMaterial.some(item=>{return !item.quota})||!remarks">中标确认</el-button> -->
        <div v-if="status===0">
            <el-button size="mini" type="primary" @click="submitQuota" v-if="materTrue">保存</el-button>
            <el-button size="mini" @click="materBtn">{{materTrue?"取消":"配额设置"}}</el-button>
        </div>
        <el-button size="mini" @click="closeBid">废标</el-button>
        <!-- <el-button size="mini">导出</el-button> -->
      </h3>
      <div class="info-div">
          <div class="info-title">
              <span>授标配额</span>
          </div>
          <el-table :data="vendorMaterial" border :span-method="arraySpanMethod">
                <el-table-column label="物料编码" align="center" prop="materialCode"></el-table-column>
                <el-table-column label="物料名称" align="center" prop="materialName"></el-table-column>
                <el-table-column label="单位" align="center" prop="materialUnit"></el-table-column>
                <el-table-column label="数量" align="center" prop="planQty"></el-table-column>
                <el-table-column label="供应商编码" align="center" prop="vendorCode"></el-table-column>
                <el-table-column label="供应商" align="center" prop="vendorName"></el-table-column>
                <!-- <el-table-column label="供应商绩效" align="center" prop="qualityTime"></el-table-column> -->
                <el-table-column label="预算金额" align="center" prop="budgetPrice"></el-table-column>
                <el-table-column label="配额" align="center" prop="suggestQuota" width="120">
                    <template slot-scope="scope">
                        <el-input  v-if="materTrue" type="number" v-model="scope.row.suggestQuota" style="width:80px;text-align:right;"></el-input>
                        <span v-else>{{scope.row.suggestQuota}}</span>
                        <span>%</span>
                    </template>
                </el-table-column>
                <el-table-column label="含税单价" align="center" prop="lastTaxPrice"></el-table-column>
                <el-table-column label="含税总价" align="center" prop="baseTotalPrice"></el-table-column>
                <el-table-column label="税率" align="center" prop="taxRate">
                    <template slot-scope="scope">
                        {{scope.row.taxRate!=null?scope.row.taxRate*100 +'%':''}} 
                    </template>
                </el-table-column>
                <el-table-column label="质保期" align="center" prop="qualityTime"></el-table-column>
                <el-table-column label="交货期" align="center" prop="deliveryDate"></el-table-column>
                <el-table-column label="备注" align="center" prop="remarks"></el-table-column>
            </el-table>
      </div>
      <div class="info-div" v-if="materTrue">
          <div class="info-title">
              <span>授标建议说明：</span>
          </div>
          <div class="info-text w100">
                <el-input v-model="remarks" type="textarea"></el-input>
          </div>
      </div>
      <div class="info-div">
            <div class="info-title">
                <span>评标结果汇总表</span>
            </div>
            <el-table :data="evalustionResult" border>
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column label="操作" align="center" width="200px">
                    <template slot-scope="scope">
                        <el-button type="text" @click="priceShow(scope.row.id)">二次报价</el-button>
                        <el-button type="text" @click="priceReturn(scope.row.id)">退还保证金</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="供应商" align="center" prop="vendorName"></el-table-column>
                <el-table-column label="技术分" align="center" prop="techScore"></el-table-column>
                <el-table-column label="技术排名" align="center" prop="techSequence"></el-table-column>
                <el-table-column label="商务分" align="center" prop="businessScore"></el-table-column>
                <el-table-column label="商务排名" align="center" prop="businessSequence"></el-table-column>
                <el-table-column label="总分" align="center" prop="totalScore"></el-table-column>
                <el-table-column label="总排名" align="center" prop="totalSequence"></el-table-column>
                <el-table-column label="总价" align="center" prop="totalPrice">
                    <template slot-scope="scope">
                        <!-- <el-button type="text">{{scope.row.quota}}</el-button> -->
                    </template>
                </el-table-column>
                <el-table-column label="价格排名" align="center" prop="priceSequence"></el-table-column>                
            </el-table>
      </div>

      <el-dialog :title="'二次报价'" :visible.sync="checkVisible" class="dialog-form" width="500px">
        <div class="info-text w100">
            <el-date-picker v-model="priceData.tenderEtimeStr" type="date" value-format="yyyy-MM-dd"
                placeholder="报价截止日期" :picker-options="pickeroptions"
                @change="()=>{priceData.evaluationEtimeStr='';}">
            </el-date-picker>
            <el-select v-model="priceData.tenderEtimeStr2" placeholder="请选择" style="width:49%">
                <el-option v-for="item in 24" :key="item" :label="item+'时'" :value="item<10?`0${item}`:item"></el-option>
            </el-select>
        </div>
        <div class="info-text w100">
            <el-date-picker v-model="priceData.evaluationEtimeStr" type="date" value-format="yyyy-MM-dd"
                placeholder="评标截止日期" :picker-options="pickeroptions1">
            </el-date-picker>
            <el-select v-model="priceData.evaluationEtimeStr2" placeholder="请选择" style="width:49%">
                <el-option v-for="item in 24" :key="item" :label="item+'时'" :value="item<10?`0${item}`:item"></el-option>
            </el-select>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="priceSubmit" :disabled="!priceData.tenderEtimeStr||!priceData.evaluationEtimeStr">提交</el-button>
            <el-button @click="checkVisible=false">返回</el-button>
        </div>
    </el-dialog>

     <!-- <el-dialog title="中标确认" :visible.sync="editVisible" width="500px">
            <el-form label-width="80px" class="demo-ruleForm">
                <el-form-item label="附件：">
                    <el-upload class="upload-file" ref="upload" :action="this.lib.config.host.system + '/common/fileupload/upload'" :data="fileData"
                    :limit="1" :on-success="res=>{uploadSuccess(res,'checkWinFileId')}" :file-list="fileList">
                    <el-button slot="trigger" type="primary">选取文件</el-button>
                    </el-upload>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="shortListConfirm" :disabled="!checkWinFileId">提交</el-button>
                    <el-button @click="editVisible=false">取消</el-button>
                </el-form-item>
            </el-form>
    </el-dialog> -->

    <!-- <el-dialog title="审核" :visible.sync="editVisible" width="500px">
            <el-form label-width="80px" class="demo-ruleForm">
                <el-form-item label="附件：">
                    <el-upload class="upload-file" ref="upload" :action="this.lib.config.host.system + '/common/fileupload/upload'" :data="fileData"
                    :limit="1" :on-success="res=>{uploadSuccess(res,'checkWinFileId')}" :file-list="fileList">
                    <el-button slot="trigger" type="primary">选取文件</el-button>
                    </el-upload>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="shortListConfirm" :disabled="!checkWinFileId">提交</el-button>
                    <el-button @click="editVisible=false">取消</el-button>
                </el-form-item>
            </el-form>
    </el-dialog> -->

    <el-dialog title="审核" :visible.sync="editVisible" width="500px">
        <div class="info-text w100">
            <span class="text-label">
                审核意见：
            </span>
            <span class="text-value">
                <el-input v-model="checkData.checkWinReason" type="textarea"></el-input>
            </span>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="()=>{checkData.checkWinStatus=1;shortListConfirm();}" :disabled="!checkData.checkWinReason">通过</el-button>
            <el-button @click="()=>{checkData.checkWinStatus=-1;shortListConfirm();}" :disabled="!checkData.checkWinReason">驳回</el-button>
        </div>
    </el-dialog>

  </div>
</template>
<script>
  import qs from 'qs';
  export default {
    props:['stepData'],
    data() {
        let _this=this;
      return {
          checkId : '',
          lastNode : '',
          showCheck: false,
          checkVisible:false,
          materTrue:false,
          editVisible:false,
          remarks:'',
          infoData:{},
          vendorMaterial:[],
          evalustionResult:[],
          priceData:{
            evaluationEtimeStr:'',
            tenderEtimeStr:'',
            evaluationEtimeStr2:'01',
            tenderEtimeStr2:'01',
          },
          checkData:{
            checkWinStatus:1,
            checkWinReason:'',
          },
          pickeroptions:{
            disabledDate(time) {
                return time.getTime() <= Date.now()- 8.64e7
            }
          },
          pickeroptions1:{
            disabledDate(time) {
                return time.getTime() < (new Date(_this.priceData.tenderEtimeStr)).getTime()
            }
          },
          checkWinFileId:'',
          fileData: {
            busAction: "BID_SET_EVA_METHOD",
            tableName: "CT_PS_BID_MAIN_EXT",
            tableId: this.$route.query.id
          },
          fileList:[],
      };
    },
    computed:{
        status(){
            return this.stepData.bidMainDto.submitWinStatus===0?0:this.stepData.bidMainDto.checkWinStatus===0?1:2;
        }
    },
    created(){
        this.queryVendorMaterial();
        this.getqueryEvalustionResult();
        this.axios.post(this.lib.config.host.bidtender + '/common/flowcheck/checkAuth', {tableId : this.$route.query.id, tableName : 'BID_TENDER', busCode : 'winBidCheck'}).then((res)=>{
          if(res.data.success && res.data.resultMap.flag == 'true'){
            this.showCheck = true;
            this.checkId = res.data.resultMap.checkId;
            this.lastNode = res.data.resultMap.lastNode;
          }
        });
    },
    methods: {
        uploadSuccess: function (response,type) {
            //附件上传成功回调
            this[type] = response.id;
        },
        getqueryEvalustionResult(){
            //获取评标结果汇总表
            this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/view/queryEvalustionResult/${this.$attrs.data.id}`).then((res) => {
                this.evalustionResult=res.data;
            })
            .catch(function (error) {
                console.log(error);
            });
        },
        queryVendorMaterial(){
            //获取授标配额
            this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/view/loadQuoteDetail2/${this.$attrs.data.id}`).then((res) => {
                this.vendorMaterial=res.data;
            })
            .catch(function (error) {
                console.log(error);
            });
        },
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            //合并单元格
            if (columnIndex === 0||columnIndex === 1||columnIndex === 2||columnIndex === 3) {
                let col=0;
                this.vendorMaterial.forEach(item=>{
                    if(item.materialId===row.materialId){
                        col++;
                    }
                })
                let index=this.vendorMaterial.findIndex(item=>{return item.materialId===row.materialId});
                if (rowIndex===index) {
                    return {
                        rowspan: col,
                        colspan: 1
                    };
                }else{
                    return {
                        rowspan: 0,
                        colspan: 0
                    };
                }
            }
        },
        closeBid({id}){
            //废标
            this.$confirm('此操作将关闭此次招标, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
            this.axios.post(`${this.lib.config.host.bidtender}/bidtender/bid/closeBid/${this.$attrs.data.id}`).then((response)=>{
                this.$message({
                    type: 'success',
                    message: '废标成功!'
                });
            });
            }).catch(() => {
            });
        },
        materBtn(){
            if(this.materTrue){
                 this.queryVendorMaterial();
                this.materTrue=false;
            }else{
                this.remarks='';
                this.materTrue=true;
            }
        },
        submitQuota(){
            //配额
            let max={};
            this.vendorMaterial.forEach(item=>{
                if(max[item.materialCode]){
                   max[item.materialCode]+=parseFloat(item.suggestQuota)
                }else{
                   max[item.materialCode]=parseFloat(item.suggestQuota)
                }
            })
            if(this.vendorMaterial.some(item=>{return !item.suggestQuota})){
                this.$message.info('配额不能为空！');
                return;
            }
            if(Object.keys(max).some(k=>{return max[k]!=100})){
                this.$message.info('同一物料分配的配额必须为100%！');
                return;
            }
            let data={
                content:this.remarks,
                quoteList:this.vendorMaterial.map(k=>{
                    return {
                        id:k.id,
                        suggestQuota:k.suggestQuota
                    }
                })
            }
            this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/bidShort/saveQuota/${this.$attrs.data.id}`,data).then((res) => {
                if(res.data.success){
                    this.queryVendorMaterial();
                    this.$message.success('分配成功');
                }
            })
            .catch(function (error) {
                console.log(error);
            });
        },
        priceShow(id){
            //二次报价弹层
            this.priceData={
                id:id,
                tenderEtimeStr:'',
                tenderEtimeStr2:'01',
                evaluationEtimeStr:'',
                evaluationEtimeStr2:'01',
            }
            this.checkVisible=true;
        },
        priceSubmit(){
            //二次报价提交
            let data={
                id:this.priceData.id,
                tenderEtimeStr:this.priceData.tenderEtimeStr+'T'+this.priceData.tenderEtimeStr2+':00:00',
                evaluationEtimeStr:this.priceData.evaluationEtimeStr+'T'+this.priceData.evaluationEtimeStr2+':00:00'
            }
            this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/bidShort/reQuote/${this.$attrs.data.id}`,data).then((res) => {
                if(res.data.success){
                    this.getqueryEvalustionResult();
                    this.$message.success('二次报价成功！');
                }
            })
            .catch(function (error) {
                console.log(error);
            });
        },
        priceReturn(id){
            //退还保证金
            this.$confirm('退还保证金?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/bidShort/bondBack/${this.$attrs.data.id}`).then((res) => {
                    if(res.data.success){
                        this.getqueryEvalustionResult();
                        this.$message.success('已退还保证金！');
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
            });
        },
        shortListShow(){
            this.checkWinFileId='';
            this.editVisible=true;
            this.checkData.checkWinStatus = 1;
            this.checkData.checkWinReason = '';

        },
        shortListConfirm(){
            this.axios.post(this.lib.config.host.bidtender + '/common/flowcheck/check', {id : this.checkId, checkStatus : this.checkData.checkWinStatus, option : this.checkData.checkWinReason}).then((res)=>{
                if(res.data.success){
                    debugger;
                    if((this.checkData.checkWinStatus == -1 && this.lastNode == 2) || this.lastNode == 1){
                        let data={
                                checkWinFileId:this.checkWinFileId
                            }
                            this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/bidShort/shortListConfirm/${this.$attrs.data.id}`, qs.stringify(data)).then((res) => {
                                if(res.data.success){
                                    this.$message.success("操作成功");
                                    this.$emit('initList');
                                    this.editVisible=false;
                                }
                            }).catch(function (error) {
                                console.log(error);
                            });
                    }else{
                        this.$message.success("操作成功");
                        this.$emit('initList');
                        this.editVisible=false;
                    }
                }
            });
            //授标确认
            // let data={
            //     checkWinFileId:this.checkWinFileId
            // }
            // this.axios.post(`${this.lib.config.host.bidtender}/bidmgt/bidShort/shortListConfirm/${this.$attrs.data.id}`, qs.stringify(data)).then((res) => {
            //     if(res.data.success){
            //         this.$message.success('已确认！');
            //     }
            // }).catch(function (error) {
            //     console.log(error);
            // });
        }
    }
  }

</script>
